<script lang="ts">
export default {
  name: 'ExamplesSonExample1'
}
</script>

<template>
  <div class="note">
    <p>1. 一个存放初始商品数据，另一个存放筛选商品数据</p>
    <p>2. v-for循环筛选商品数据，显示在页面便于用户查看</p>
    <p>3. `v-model`捕获input表单输入的关键词</p>
    <p>4. 筛选可以用`计算属性`实现，`filter函数`筛选，`toLowerCase`不区分大小写，includes``查找</p>
  </div>
  <div class="test">
    <div id="app">
      <h1>Product List</h1>
      <div class="search-box">
        <input type="text" v-model="searchTerm" placeholder="please input the product you want" />
      </div>
      <ul v-for="product in filterProducts" :key="product.id">
        <li>{{ product.name }} - <span class="price">${{  product.price }}</span></li>
      </ul>
    </div>
  </div>
</template>

<!-- <script src="https://unpkg.com/vue@next"></script> -->
<script setup lang="ts">
import { computed, ref } from 'vue'

// 初始化商品数据
const products = ref([
  { id: 1, name: 'Apple', price: 0.99 },
  { id: 2, name: 'Banana', price: 0.59 },
  { id: 3, name: 'Orange', price: 0.79 },
  { id: 4, name: 'Milk', price: 1.99 },
  { id: 5, name: 'Bread', price: 1.5 }
  // 更多商品...
])

// 用户输入关键词
const searchTerm = ref('')

// 计算属性：根据搜索词筛选商品
const filterProducts = computed(()=>{
  return products.value.filter(product=>product.name.toLowerCase().includes(searchTerm.value.toLowerCase()))
})
</script>

<style scoped>
/* body {
  font-family: Arial, sans-serif;
  margin: 0;
  padding: 0;
  background-color: #f4f4f4;
}

#app {
  max-width: 600px;
  margin: 50px auto;
  padding: 20px;
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
} */

h1 {
  text-align: center;
  color: #333;
}

.search-box {
  display: flex;
  justify-content: center;
  margin-bottom: 20px;
}

input[type='text'] {
  padding: 10px;
  font-size: 16px;
  border: 1px solid #ddd;
  border-radius: 4px;
  width: 100%;
  max-width: 500px;
}

input[type='text']:focus {
  outline: none;
  border-color: #007bff;
}

ul {
  list-style: none;
  padding: 0;
}

li {
  padding: 10px;
  border-bottom: 1px solid #eee;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

li:last-child {
  border-bottom: none;
}

.price {
  font-weight: bold;
  color: #007bff;
}

@media (max-width: 600px) {
  .search-box {
    flex-direction: column;
  }

  input[type='text'] {
    margin-bottom: 10px;
  }
}

body {
  padding: 20px;
}
h2 {
  color: red;
}
h3 {
  color: rgb(220, 85, 85);
}
h4 {
  color: skyblue;
}
.hr-style {
  border: 3px solid gold;
  margin: 20px 0;
}
.group {
  border: 4px solid rgb(102, 215, 85);
  margin: 10px;
  padding: 10px;
}
.group3 {
  border: 2px solid rgb(64, 163, 199);
  margin: 10px;
  padding: 10px;
}
.note {
  margin: 10px;
  padding: 10px;
  border: 3px solid rgb(221, 121, 202);
}
.note p {
  color: rgb(226, 106, 204);
  font-weight: bold;
}
</style>
